package com.sgb.kjwl.view.ui.adapter.transport;

import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.CargoEntity;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.NumberUtil;

import java.util.List;

/**
 * @author: 杨畔
 * @date: 2020/12/30 10:15
 * @Description: 货源发布选择历史货源适配器
 */
public class CargoPublishHistoryAdapter extends BaseQuickAdapter<CargoEntity, BaseViewHolder> {
    public CargoPublishHistoryAdapter(@Nullable List<CargoEntity> data) {
        super(R.layout.item_cargo_publish_history, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CargoEntity item) {
        helper.setText(R.id.tv_item_cp_history_place, item.getGoodsSourcePlace() == null
                ? ""
                : String.format("%s %s ➞ %s %s",
                item.getGoodsSourcePlace().getCityName(),
                item.getGoodsSourcePlace().getDistrictName(),
                item.getGoodsSourcePlace().getDestCityName(),
                item.getGoodsSourcePlace().getDestDistrictName()))
                .setText(R.id.tv_item_cp_history_msg, item.getPattern() == 1
                        ? String.format("运费单价 %s元", NumberUtil.trim0(item.getFreight()))
                        : String.format("信息费 %s元", NumberUtil.trim0(item.getMessagePrice())))
                .setText(R.id.tv_item_cp_history_state, getGoodsStatus(item));
        //根据货源类型设置运费
        if (item.getPattern() == 1) {
            helper.setText(R.id.tv_item_cp_history_freight, String.format(mContext.getString(R.string.app_the_freight_combined)+" %s元", NumberUtil.trim0(item.getAllFreight())));
        } else {
            helper.setText(R.id.tv_item_cp_history_freight,
                    item.getFreightType() == null || item.getFreightType() == 0
                            ? String.format(mContext.getString(R.string.app_the_freight_combined)+" %s元", NumberUtil.trim0(item.getAllFreight()))
                            : mContext.getString(R.string.app_the_freight_combined)+" 电议");
        }
        //车辆要求
        StringBuilder builder = new StringBuilder();
        if (!TextUtils.isEmpty(item.getCarTypeName())) {
            builder.append(" ").append(String.format("%s", item.getCarTypeName().replaceAll(",", "/")));
        }
        if (!TextUtils.isEmpty(item.getCarLenName())) {
            builder.append(" ").append(String.format("%s", item.getCarLenName().replaceAll(",", "/")));
        }
        //货物信息
        builder.append(" ").append(CargoConstant.getLastGoodsTypeName(item.getGoodsTypeName(), item.getSecondGoodsTypeName()));
        if (NumberUtil.convertToDouble(item.getWeight()) != 0) {
            builder.append("/").append(NumberUtil.trim0(item.getWeight())).append("吨");
        }
        if (NumberUtil.convertToDouble(item.getVolume()) != 0) {
            builder.append("/").append(NumberUtil.trim0(item.getVolume())).append("方");
        }
        helper.setText(R.id.tv_item_cp_history_info, builder.length() == 0 ? "" : builder.substring(1));
    }

    /**
     * 获取货源状态
     */
    private String getGoodsStatus(CargoEntity item) {
        String status = "";
        //货源状态 0 待接单/竞价中  1 已成交/同意已结束   2 已失效/截止已结束  3 冻结(付款中) 4 已下架 5 已成交/已失效
        if (item.getPattern() == 0) {
            switch (item.getState()) {
                case CargoConstant.NormalCargoMode.MODE_PENDING_ORDER:
                    status = "待接单";
                    break;
                case CargoConstant.NormalCargoMode.MODE_DEAL_DOWN:
                    status = "已成交";
                    break;
                case CargoConstant.NormalCargoMode.MODE_EXPIRED:
                    status = "已失效";
                    break;
                case CargoConstant.NormalCargoMode.MODE_PAYING:
                    status = "付款中";
                    break;
                case CargoConstant.NormalCargoMode.MODE_DOWN:
                    status = "已下架";
                    break;
            }
        } else if (item.getPattern() == 1) {
            switch (item.getState()) {
                case CargoConstant.BiddingCargoMode.MODE_BIDDING:
                    status = "竞价中";
                    break;
                case CargoConstant.BiddingCargoMode.MODE_DEAL_DOWN:
                    status = "已成交";
                    break;
                case CargoConstant.BiddingCargoMode.MODE_EXPIRED:
                    status = "已失效";
                    break;
                case CargoConstant.BiddingCargoMode.MODE_DEADLINE:
                    status = "报价截止";
                    break;
                case CargoConstant.BiddingCargoMode.MODE_DOWN:
                    status = "已下架";
                    break;
            }
        }
        return status;
    }
}
